package com.umbrella.worker.service.impl;

import java.util.Calendar;

import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.umbrella.worker.dao.WNeedMapper;
import com.umbrella.worker.dto.NeedDO;

import com.umbrella.worker.entity.WNeed;
import com.umbrella.worker.entity.WNeedExample;

import com.umbrella.worker.query.NeedQuery;
import com.umbrella.worker.result.ResultDO;
import com.umbrella.worker.result.ResultSupport;
import com.umbrella.worker.service.INeedService;
import com.umbrella.worker.util.BeanUtilsExtends;
import com.umbrella.worker.util.StringUtil;




@Service("needService")
public class NeedServiceImpl extends BaseServiceImpl implements INeedService {
	
	private static Logger logger = Logger.getLogger(OrderServiceImpl.class);
	
	@Autowired
	private WNeedMapper needMapper;
	
	@Transactional
	@Override
	public ResultDO create(NeedDO needDO) {
		WNeed need = new WNeed();			
		
		ResultSupport result = BeanUtilsExtends.copy(need, needDO);
		if(!result.isSuccess()) {
			return result;
		}
		int recordNum = -1;
		
		need.setDatalevel(1);
		need.setStatus(1);
		need.setModifiAuthor(need.getCreateAuthor());
		need.setCreateTime(Calendar.getInstance().getTime());
		need.setModifiTime(Calendar.getInstance().getTime());
		try {
			recordNum = needMapper.insertSelective(need);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			logger.error("[obj:need][opt:create][msg:" + e.getMessage()
			+ "]");
			return result;
		}
		
		if(recordNum == 1) {
			result.setModel(ResultDO.FIRST_MODEL_KEY, need.getId());
		} else {
			result.setSuccess(false);
		}
		return result;
	}
	
	@Transactional
	@Override
	public ResultDO modify(NeedDO needDO) {
		WNeed need = new WNeed();

		ResultSupport result = BeanUtilsExtends.copy(need, needDO);
		// 拷贝失败
		if (!result.isSuccess()) {
			return result;
		}
		need.setModifiTime(Calendar.getInstance().getTime());
		int recordNum = -1;
		try {
			recordNum = needMapper.updateByPrimaryKeySelective(need);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			logger.error("[obj:need][opt:modifi][msg:" + e.getMessage()
					+ "]");
			return result;
		}
		if (recordNum < 1) {
			result.setSuccess(false);
		}

		return result;
	}
	
	@Transactional
	@Override
	public ResultDO updateStatus(NeedDO needDO) {
		// TODO Auto-generated method stub
		return null;
	}

	@Transactional
	@Override
	public ResultDO remove(int needId) {
		
		ResultSupport result = new ResultSupport();
		
		WNeed need = new WNeed();
		
		if(StringUtil.isGreatOne(needId)) {
			need.setId(needId);
		} else {
		    result.setSuccess(false);
		    return result;
		}
		need.setDatalevel(-1);
		int recordNum = -1;
		try {
			recordNum = needMapper.updateByPrimaryKeySelective(need);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:need][opt:remove][msg:"+e.getMessage()+"]");
	        return result;
		}
		if (recordNum != 1) {
			result.setSuccess(false);
		}
		return result;
	}
	
	@Transactional
	@Override
	public ResultDO list(NeedQuery needQuery) {
		
		ResultSupport result = new ResultSupport();
		
		WNeedExample example = new WNeedExample();
		WNeedExample.Criteria c = example.createCriteria();
	
		
		if(StringUtil.isGreatOne(needQuery.getNeedType())) {
			c.andWNeedTypeEqualTo(needQuery.getNeedType());
		}
		
		if(StringUtil.isGreatOne(needQuery.getStatus())) {
			c.andStatusEqualTo(needQuery.getStatus());
		}
		if(StringUtil.isGreatOne(needQuery.getSupplierId())) {
			c.andWSupplierIdEqualTo(needQuery.getSupplierId());
		}
		if(StringUtil.isGreatOne(needQuery.getMemberId())) {
			c.andWMemberIdEqualTo(needQuery.getMemberId());
		}
		if(StringUtil.isNotEmpty(needQuery.getOrderByClause())) {	
			example.setOrderByClause(" " + needQuery.getOrderByClause() + " " + needQuery.getSort());
		} else {
			example.setOrderByClause(" CREATE_TIME DESC");
		}
		
		c.andDatalevelEqualTo(1);
		
		if(needQuery.isPage()) {
			long count = 0;
			try {
				count = needMapper.countByExample(example);
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        e.printStackTrace();
		        logger.error("[obj:need][opt:get][msg:"+e.getMessage()+"]");
		        return result;
			}
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
			int pageNO = needQuery.getPageNO();
			if(pageNO > 0) {
				pageNO = pageNO -1;
			}
			String pageByClause = " limit " + (pageNO * needQuery.getPageRows())
					+ "," + needQuery.getPageRows();
			
			example.setPageByClause(pageByClause);
		}
		
		List<WNeed> list = null;
		
		try {
			list = needMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:need][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<NeedDO> needList = getNeedDOList(list);

		result.setModel(ResultSupport.FIRST_MODEL_KEY, needList);
		
		return result;
	}
	
	public ResultDO get(int needId) {
		
		ResultSupport result = new ResultSupport();
		
		WNeed need = null;
		if(!StringUtil.isGreatOne(needId)) {
			 result.setSuccess(false);
			 return result;
		} 
		
		try {
			need = needMapper.selectByPrimaryKey(needId);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:need][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		NeedDO needDO = getNeedDO(need);
		if(needDO != null) {
		} else {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			return result;
		}

		result.setModel(ResultSupport.FIRST_MODEL_KEY, needDO);
		return result;
	}
	
	@Override
	public ResultDO getByOrderId(Integer orderId) {
		ResultSupport result = new ResultSupport();
		
		WNeed need = null;
		if(!StringUtil.isGreatOne(orderId)) {
			 result.setSuccess(false);
			 return result;
		} 
		
		try {
			//need = needMapper.selectByOderId(orderId);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:need][opt:getByOrderId][msg:"+e.getMessage()+"]");
	        return result;
	}
		
		NeedDO needDO = getNeedDO(need);
		if(needDO != null) {
		} else {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			return result;
		}

		result.setModel(ResultSupport.FIRST_MODEL_KEY, needDO);
		return result;
	}
	
	@Transactional
	@Override
	public ResultDO confirm(NeedDO needDO) {

		ResultSupport result = new ResultSupport();
		
		WNeed need = new WNeed();
		
		result = BeanUtilsExtends.copy(need, needDO);
		
		if(!result.isSuccess()) {
			 return result;
		} 
		need.setModifiTime(Calendar.getInstance().getTime());
		int recordNum = -1;
		try {
			recordNum =needMapper.updateByPrimaryKeySelective(need);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:need][opt:confirm][msg:"+e.getMessage()+"]");
	        e.printStackTrace();
	        return result;
		}
		
		if(recordNum < 1) {
			result.setSuccess(false);
			return result;
		}	
		return result;
	}
	
	@Transactional
	@Override
	public ResultDO waitList(NeedQuery needQuery) {

		ResultSupport result = new ResultSupport();
		WNeedExample example = new WNeedExample();
		WNeedExample.Criteria c = example.createCriteria();
		
		if(needQuery.getStartDate()!=null  ) {
			c.andCreateTimeGreaterThanOrEqualTo(needQuery.getStartDate());
		}
		if(needQuery.getEndDate()!=null){
			c.andCreateTimeLessThanOrEqualTo(needQuery.getEndDate());
		}
		if(needQuery.getSupplierId()!=null){
			c.andWSupplierIdEqualTo(needQuery.getSupplierId());
		}
		
		if(needQuery.getSupplierId()!=null){
		}else{
			c.andStatusEqualTo(1);
		}
		
		example.setOrderByClause(" CREATE_TIME DESC");
		
		c.andDatalevelEqualTo(1);
		
		if(needQuery.isPage()) {
			long count = 0;
			try {
				count = needMapper.countByExample(example);
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        e.printStackTrace();
		        logger.error("[obj:need][opt:get][msg:"+e.getMessage()+"]");
		        return result;
			}
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
			int pageNO = needQuery.getPageNO();
			if(pageNO > 0) {
				pageNO = pageNO -1;
			}
			String pageByClause = " limit " + (pageNO * needQuery.getPageRows())
					+ "," + needQuery.getPageRows();
			
			example.setPageByClause(pageByClause);
		}
		
		List<WNeed> list = null;
		
		try {
			list = needMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:need][opt:waitList][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<NeedDO> needList = getNeedDOList(list);
		
		result.setModel(ResultSupport.FIRST_MODEL_KEY, needList);
		
		return result;
	}
	
	
}

